package ajsynchro;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import polyglot.ast.Block;
import polyglot.ast.Expr;
import polyglot.ast.Node;
import polyglot.ast.NodeFactory;
import polyglot.ast.Stmt;
import polyglot.ast.Term;
import polyglot.ext.jl.ast.Stmt_c;
import polyglot.ext.jl.parse.Name;
import polyglot.types.TypeSystem;
import polyglot.util.Position;
import polyglot.visit.CFGBuilder;

import xaj.xc.ast.NodeXAJ;

public class Await extends Stmt_c implements Stmt, NodeXAJ, xaj.lang.Map {
	
	/*
	@grammar extends statement {
		Await -> "await" id = simple_name "while" "(" e = expression ")" ";";
	}*/

	private Map map;
	
	public Await(Position pos) {
		super(pos);
		this.map = new HashMap();
		System.out.println("Criou um objeto Await");
	}

	public List acceptCFG(CFGBuilder v, List succs) {
		return succs;
	}

	public Term entry() {
		return this;
	}

	public Object get(String name) {
		return map.get(name);
	}
	

	public boolean add(String name, Object value) {
		if(!map.containsKey(name)) {
			map.put(name, value);
			return true;
		} else
			return false;
	}

	public Iterator iterator() {
		return map.keySet().iterator();
	}

	public void set(String name, Object value) {
		if(map.containsKey(name)) {
			map.keySet();
			map.put(name, value);
		}
	}
	
	public Node desugar(NodeFactory nf, TypeSystem ts) {
		Name name = (Name) get("id");
		Expr expr = (Expr) get("e");
		Block body = nf.Block(position(),
				nf.Eval(position(), nf.Call(position(), name.toExpr(), "await")));
		Stmt stmt = nf.While(position(), expr, body);	
		return stmt;
	}
}